﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Shared;
using System.Reflection;
using System.Runtime.InteropServices;
using Excel;
using System.IO;
using LibHuman;


namespace Human
{
	/// <summary>
	/// Summary description for Freport.
	/// </summary>
	public class frmReport : System.Windows.Forms.Form
	{
        ExportToExcel exp;
        LibHuman.AccessData m;
        ReportDocument oRpt;
		Excel.Application oxl;
		Excel._Workbook owb;
		Excel._Worksheet osheet;
        frmMain2005 f; //= new frmMain2005()
		public bool bPrinter;
        private string msg, msg1 = "", ReportFile = "", ngaythangnam = "", ngayin = "", bophan = "",s_mmyy="",nguoi_lb="";
		private DataSet ds=new DataSet();
		private System.Data.DataTable dt=new System.Data.DataTable();
		private CrystalDecisions.Windows.Forms.CrystalReportViewer Report;
		private int d01,d02,d03,d04,d05,d06,d07,d08,d09,d10;
		private int i_nhom=1,i_soluong_le=0,i_dongia_le=0,i_thanhtien_le=0;
		private string c1,c2,c3,c4,c5,c6,c7,c8,c9,c10;
        private bool b_145 = false, nhanluckhoasan=false, b_benhan = false, b_bienlai = false, b_baohiem = false, bDoc = false, bia = false, hopdong = false;
		
        private System.Windows.Forms.NumericUpDown banin;
		private System.Windows.Forms.NumericUpDown den;
		private System.Windows.Forms.Label label3;
		private System.Windows.Forms.NumericUpDown tu;
        private System.Windows.Forms.Label label2;
        private System.Windows.Forms.Button butIn;
        private System.Windows.Forms.Button butExcel;
        private System.Windows.Forms.Button butKetthuc;
        private System.Windows.Forms.Label label4;
		/// <summary>
		/// Required designer variable.
		/// </summary>
		private System.ComponentModel.Container components = null;

		public frmReport(AccessData acc,System.Data.DataTable ta,string report,string s1,string s2,string s3,string s4,string s5,string s6,string s7,string s8,string s9,string s10)
		{
			InitializeComponent();
			m=acc;
			dt=ta;
			c1=s1;
			c2=s2;
			c3=s3;
			c4=s4;
			c5=s5;
			c6=s6;
			c7=s7;
			c8=s8;
			c9=s9;
			c10=s10;
			b_baohiem=true;
			ReportFile=report;
			this.Text=report;
		}
		public frmReport(DataSet dset,string s1,string s2,string report,string ten_nd)
		{
			InitializeComponent();
			ds=dset;
			c1=s1;c2=s2;
			b_bienlai=true;
			ReportFile=report;
			this.Text=report;
            nguoi_lb = m.get_tennvdangnhap(ten_nd.Trim());
		}
		public frmReport(AccessData acc,DataSet dset,string mMsg,string report,string ten_nd)
		{
			InitializeComponent();
			m=acc;
			ds=dset;
			msg=mMsg;
			ReportFile=report;
			this.Text=report;
            nguoi_lb = m.get_tennvdangnhap(ten_nd.Trim());
		}

		public frmReport(AccessData acc,DataSet dset,string mMsg,string report,bool doc,string ten_nd)
		{
			InitializeComponent();
			m=acc;
			ds=dset;
			msg=mMsg;
			ReportFile=report;
			bDoc=doc;
			this.Text=report;
            nguoi_lb = m.get_tennvdangnhap(ten_nd.Trim());
		}
        public frmReport(AccessData acc, DataSet dset, string mMsg, string report, bool doc,bool bHopdong,string ten_nd)
        {
            InitializeComponent();
            m = acc;
            ds = dset;
            msg = mMsg;
            ReportFile = report;
            bDoc = doc;
            hopdong = bHopdong;
            this.Text = report;
            nguoi_lb = m.get_tennvdangnhap(ten_nd.Trim());
        }

        public frmReport(AccessData acc, DataSet dset, string mMsg, string report, bool doc,int done,string ten_nd)
        {
            InitializeComponent();
            m = acc;
            ds = dset;
            dt = dset.Tables[0];
            msg = mMsg;
            ReportFile = report;
            bDoc = doc;
            butIn.Enabled = done == 0;
            butExcel.Enabled = done == 0;
            Report.ShowExportButton = done == 0;
            Report.ShowPrintButton = done == 0;
            this.Text = report;
            nguoi_lb = m.get_tennvdangnhap(ten_nd.Trim());
        }

		public frmReport(AccessData acc,DataSet dset,string mMsg,string report,int i01,int i02,int i03,int i04,int i05,int i06,int i07,int i08,int i09,int i10)
		{
			InitializeComponent();
			m=acc;
			ds=dset;
			msg=mMsg;
			d01=i01;
			d02=i02;
			d03=i03;
			d04=i04;
			d05=i05;
			d06=i06;
			d07=i07;
			d08=i08;
			d09=i09;
			d10=i10;
			b_145=true;
			ReportFile=report;
			this.Text=report;
		}		

		protected override void Dispose( bool disposing )
		{
			if( disposing )
			{
				if(components != null)
				{
					components.Dispose();
				}
			}
			base.Dispose( disposing );
		}

		#region Windows Form Designer generated code
		/// <summary>
		/// Required method for Designer support - do not modify
		/// the contents of this method with the code editor.
		/// </summary>
		private void InitializeComponent()
		{
            System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(frmReport));
            this.Report = new CrystalDecisions.Windows.Forms.CrystalReportViewer();
            this.banin = new System.Windows.Forms.NumericUpDown();
            this.den = new System.Windows.Forms.NumericUpDown();
            this.label3 = new System.Windows.Forms.Label();
            this.tu = new System.Windows.Forms.NumericUpDown();
            this.label2 = new System.Windows.Forms.Label();
            this.butIn = new System.Windows.Forms.Button();
            this.butExcel = new System.Windows.Forms.Button();
            this.butKetthuc = new System.Windows.Forms.Button();
            this.label4 = new System.Windows.Forms.Label();
            ((System.ComponentModel.ISupportInitialize)(this.banin)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.den)).BeginInit();
            ((System.ComponentModel.ISupportInitialize)(this.tu)).BeginInit();
            this.SuspendLayout();
            // 
            // Report
            // 
            this.Report.ActiveViewIndex = -1;
            this.Report.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle;
            this.Report.DisplayGroupTree = false;
            this.Report.Location = new System.Drawing.Point(-1, 0);
            this.Report.Name = "Report";
            this.Report.SelectionFormula = "";
            this.Report.ShowCloseButton = false;
            this.Report.Size = new System.Drawing.Size(774, 358);
            this.Report.TabIndex = 9;
            this.Report.ViewTimeSelectionFormula = "";
            // 
            // banin
            // 
            this.banin.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.banin.Location = new System.Drawing.Point(374, 5);
            this.banin.Minimum = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.banin.Name = "banin";
            this.banin.Size = new System.Drawing.Size(34, 21);
            this.banin.TabIndex = 1;
            this.banin.Value = new decimal(new int[] {
            1,
            0,
            0,
            0});
            this.banin.KeyDown += new System.Windows.Forms.KeyEventHandler(this.banin_KeyDown);
            // 
            // den
            // 
            this.den.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.den.Location = new System.Drawing.Point(535, 5);
            this.den.Name = "den";
            this.den.Size = new System.Drawing.Size(44, 21);
            this.den.TabIndex = 5;
            this.den.KeyDown += new System.Windows.Forms.KeyEventHandler(this.banin_KeyDown);
            // 
            // label3
            // 
            this.label3.AutoSize = true;
            this.label3.BackColor = System.Drawing.Color.Transparent;
            this.label3.ForeColor = System.Drawing.Color.DarkBlue;
            this.label3.Location = new System.Drawing.Point(505, 9);
            this.label3.Name = "label3";
            this.label3.Size = new System.Drawing.Size(32, 13);
            this.label3.TabIndex = 4;
            this.label3.Text = "đến :";
            this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // tu
            // 
            this.tu.Font = new System.Drawing.Font("Tahoma", 8.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            this.tu.Location = new System.Drawing.Point(458, 5);
            this.tu.Name = "tu";
            this.tu.Size = new System.Drawing.Size(45, 21);
            this.tu.TabIndex = 3;
            this.tu.KeyDown += new System.Windows.Forms.KeyEventHandler(this.banin_KeyDown);
            // 
            // label2
            // 
            this.label2.AutoSize = true;
            this.label2.BackColor = System.Drawing.Color.Transparent;
            this.label2.ForeColor = System.Drawing.Color.DarkBlue;
            this.label2.Location = new System.Drawing.Point(406, 9);
            this.label2.Name = "label2";
            this.label2.Size = new System.Drawing.Size(53, 13);
            this.label2.TabIndex = 2;
            this.label2.Text = "Từ trang :";
            this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            // 
            // butIn
            // 
            this.butIn.ForeColor = System.Drawing.Color.DarkBlue;
            this.butIn.Image = ((System.Drawing.Image)(resources.GetObject("butIn.Image")));
            this.butIn.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butIn.Location = new System.Drawing.Point(582, 4);
            this.butIn.Name = "butIn";
            this.butIn.Size = new System.Drawing.Size(45, 22);
            this.butIn.TabIndex = 6;
            this.butIn.Text = "In";
            this.butIn.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butIn.UseVisualStyleBackColor = true;
            this.butIn.Click += new System.EventHandler(this.butIn_Click);
            // 
            // butExcel
            // 
            this.butExcel.ForeColor = System.Drawing.Color.DarkBlue;
            this.butExcel.Image = ((System.Drawing.Image)(resources.GetObject("butExcel.Image")));
            this.butExcel.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butExcel.Location = new System.Drawing.Point(629, 4);
            this.butExcel.Name = "butExcel";
            this.butExcel.Size = new System.Drawing.Size(60, 22);
            this.butExcel.TabIndex = 7;
            this.butExcel.Text = "Excel";
            this.butExcel.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butExcel.UseVisualStyleBackColor = true;
            this.butExcel.Click += new System.EventHandler(this.butExcel_Click);
            // 
            // butKetthuc
            // 
            this.butKetthuc.DialogResult = System.Windows.Forms.DialogResult.Cancel;
            this.butKetthuc.ForeColor = System.Drawing.Color.DarkBlue;
            this.butKetthuc.Image = ((System.Drawing.Image)(resources.GetObject("butKetthuc.Image")));
            this.butKetthuc.ImageAlign = System.Drawing.ContentAlignment.MiddleLeft;
            this.butKetthuc.Location = new System.Drawing.Point(691, 4);
            this.butKetthuc.Name = "butKetthuc";
            this.butKetthuc.Size = new System.Drawing.Size(73, 22);
            this.butKetthuc.TabIndex = 8;
            this.butKetthuc.Text = "Kết thúc";
            this.butKetthuc.TextAlign = System.Drawing.ContentAlignment.MiddleRight;
            this.butKetthuc.UseVisualStyleBackColor = true;
            this.butKetthuc.Click += new System.EventHandler(this.butKetthuc_Click);
            // 
            // label4
            // 
            this.label4.AutoSize = true;
            this.label4.BackColor = System.Drawing.Color.Transparent;
            this.label4.ForeColor = System.Drawing.Color.DarkBlue;
            this.label4.Location = new System.Drawing.Point(312, 9);
            this.label4.Name = "label4";
            this.label4.Size = new System.Drawing.Size(58, 13);
            this.label4.TabIndex = 0;
            this.label4.Text = "&Số bản in :";
            // 
            // frmReport
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(1016, 713);
            this.Controls.Add(this.label4);
            this.Controls.Add(this.butKetthuc);
            this.Controls.Add(this.butExcel);
            this.Controls.Add(this.butIn);
            this.Controls.Add(this.den);
            this.Controls.Add(this.tu);
            this.Controls.Add(this.banin);
            this.Controls.Add(this.label3);
            this.Controls.Add(this.label2);
            this.Controls.Add(this.Report);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.KeyPreview = true;
            this.Name = "frmReport";
            this.ShowInTaskbar = false;
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Quản lý nhân sự";
            this.WindowState = System.Windows.Forms.FormWindowState.Maximized;
            this.Closed += new System.EventHandler(this.frmReport_Closed);
            this.KeyDown += new System.Windows.Forms.KeyEventHandler(this.frmReport_KeyDown);
            this.Load += new System.EventHandler(this.frmReport_Load);
            ((System.ComponentModel.ISupportInitialize)(this.banin)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.den)).EndInit();
            ((System.ComponentModel.ISupportInitialize)(this.tu)).EndInit();
            this.ResumeLayout(false);
            this.PerformLayout();

		}
		#endregion

		private void frmReport_Load(object sender, System.EventArgs e)
		{
            this.Size = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width, Screen.PrimaryScreen.WorkingArea.Height);
			this.Report.Size = new System.Drawing.Size(Screen.PrimaryScreen.WorkingArea.Width,Screen.PrimaryScreen.WorkingArea.Height);
			i_soluong_le=m.d_soluong_le(i_nhom);
			i_thanhtien_le=m.d_thanhtien_le(i_nhom);
			this.Tag=System.Environment.CurrentDirectory.ToString();
			if (hopdong) Preview_hd();
			else PreviewReport();
            this.Text = ReportFile;
		}
        private void Preview_hd()
        {
            try
            {
                string m_diachi = "";
                DataSet ds1 = new DataSet();
                ds1 = m.get_data("select a.*,b.tentt from nhansu.btdquan a,nhansu.btdtt b where maqu='"+m.Maqu().ToString()+"' and a.matt=b.matt");
                foreach (DataRow r in ds1.Tables[0].Rows)
                    m_diachi = r["tenquan"].ToString().Trim() + ", " + r["tentt"].ToString().Trim();
                oRpt = new ReportDocument();
                oRpt.Load("..\\report\\" + ReportFile, OpenReportMethod.OpenReportByTempCopy);
                oRpt.SetDataSource(ds);
                oRpt.DataDefinition.FormulaFields["SoYTe"].Text = "'" + m.Syte().ToUpper() + "'";
                oRpt.DataDefinition.FormulaFields["BenhVien"].Text = "'" + m.Tenbv.ToUpper() + "'";
                oRpt.DataDefinition.FormulaFields["title"].Text = "'" + msg + "'";
                try
                {
                    oRpt.DataDefinition.FormulaFields["Giamdoc"].Text = "'" + m.Giamdoc() + "'";
                    oRpt.DataDefinition.FormulaFields["Dienthoai"].Text = "'" + m.Dienthoai() + "'";
                    oRpt.DataDefinition.FormulaFields["Diachi"].Text = "'" + m.Diachi() + "'";
                    oRpt.DataDefinition.FormulaFields["tentt"].Text = "'" + m.ten_tt() + "'";
                    oRpt.DataDefinition.FormulaFields["nguoi_lb"].Text = "'" + nguoi_lb.Trim() + "'";
                    oRpt.DataDefinition.FormulaFields["tp_tochuc"].Text = "'" + m.tp_tochuc() + "'";                    
                    oRpt.DataDefinition.FormulaFields["fax"].Text = "'" + m.fax() + "'";
                    oRpt.DataDefinition.FormulaFields["website"].Text = "'" + m.website() + "'";
                }
                catch
                { }             
                Report.ReportSource = oRpt;
            }
            catch (Exception e)
            {
                MessageBox.Show("The following error was discovered: '" + e.Message + "'. It was occured in '" + e.StackTrace + "'", "Report Viewer", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }	

		public void PreviewReport()
		{
			try
			{                
                oRpt=new ReportDocument();
				oRpt.Load("..\\report\\"+ReportFile);//,OpenReportMethod.OpenReportByDefault);
				oRpt.SetDataSource(ds);
                oRpt.DataDefinition.FormulaFields["SoYTe"].Text = "'" + m.Syte().ToUpper() + "'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+m.Tenbv.ToUpper()+"'";
                oRpt.DataDefinition.FormulaFields["title"].Text = "'" + msg + "'";
                try
                {
                    oRpt.DataDefinition.FormulaFields["Giamdoc"].Text = "'" + m.Giamdoc() + "'";
                    oRpt.DataDefinition.FormulaFields["Dienthoai"].Text = "'" + m.Dienthoai() + "'";
                    oRpt.DataDefinition.FormulaFields["Diachi"].Text = "'" + m.Diachi() + "'";
                    oRpt.DataDefinition.FormulaFields["nguoi_lb"].Text = "'" + nguoi_lb.Trim() + "'";
                    oRpt.DataDefinition.FormulaFields["tentt"].Text = "'" + m.ten_tt() + "'";
                    oRpt.DataDefinition.FormulaFields["tp_tochuc"].Text = "'" + m.tp_tochuc() + "'";                    
                    oRpt.DataDefinition.FormulaFields["fax"].Text = "'" + m.fax() + "'";
                    oRpt.DataDefinition.FormulaFields["website"].Text = "'" + m.website() + "'";
                    oRpt.DataDefinition.FormulaFields["Maso"].Text = "'" + m.Maso + "'";                    
                }
                catch
                { }
                if (msg1 != "")
                    oRpt.DataDefinition.FormulaFields["msg"].Text = "'" + msg1 + "'";
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}		

		private void Report145()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				oRpt.DataDefinition.FormulaFields["SoYTe"].Text="'"+m.Syte()+"'";
				oRpt.DataDefinition.FormulaFields["BenhVien"].Text="'"+m.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["TenBenhAn"].Text="'"+msg+"'";
				oRpt.DataDefinition.FormulaFields["d01"].Text="'"+d01.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d02"].Text="'"+d02.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d03"].Text="'"+d03.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d04"].Text="'"+d04.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d05"].Text="'"+d05.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d06"].Text="'"+d06.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d07"].Text="'"+d07.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d08"].Text="'"+d08.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d09"].Text="'"+d09.ToString()+"'";
				oRpt.DataDefinition.FormulaFields["d10"].Text="'"+d10.ToString()+"'";
//				oRpt.PrintOptions.PaperSize=PaperSize.PaperA4;
//				oRpt.PrintOptions.PaperOrientation=PaperOrientation.Landscape;
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}	
		private void Bienlai()
		{
			try
			{
				oRpt=new ReportDocument();
				oRpt.Load("..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(ds);
				if (c1!="")
				{
					oRpt.DataDefinition.FormulaFields["s_nguoithu"].Text="'"+c1+"'";
					oRpt.DataDefinition.FormulaFields["s_sovaovien"].Text="'"+c2+"'";
				}
				//oRpt.PrintOptions.PaperSize=PaperSize.PaperA4;
				//oRpt.PrintOptions.PaperOrientation=PaperOrientation.Landscape;
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}		

		private void frmReport_Closed(object sender, System.EventArgs e)
		{
			System.Environment.CurrentDirectory=this.Tag.ToString();
		}	

		private void Baohiem()
		{
			try
			{
				string phieulinh="d_phieulanh_yc.rpt,d_phieulanh_yc_dg.rpt,d_phieulanh.rpt,d_phieulanh_dg.rpt,d_pldoc.rpt";
				oRpt=new ReportDocument();
				oRpt.Load("..\\report\\"+ReportFile,OpenReportMethod.OpenReportByTempCopy);
				oRpt.SetDataSource(dt);
				oRpt.DataDefinition.FormulaFields["soyte"].Text="'"+m.Syte()+"'";
				oRpt.DataDefinition.FormulaFields["benhvien"].Text="'"+m.Tenbv+"'";
				oRpt.DataDefinition.FormulaFields["c1"].Text="'"+c1+"'";
				oRpt.DataDefinition.FormulaFields["c2"].Text="'"+c2+"'";
				oRpt.DataDefinition.FormulaFields["c3"].Text="'"+c3+"'";
				oRpt.DataDefinition.FormulaFields["c4"].Text="'"+c4+"'";
				oRpt.DataDefinition.FormulaFields["c5"].Text="'"+c5+"'";
				oRpt.DataDefinition.FormulaFields["c6"].Text="'"+c6+"'";
				oRpt.DataDefinition.FormulaFields["c7"].Text="'"+c7+"'";
				oRpt.DataDefinition.FormulaFields["c8"].Text="'"+c8+"'";
				oRpt.DataDefinition.FormulaFields["c9"].Text="'"+c9+"'";
				oRpt.DataDefinition.FormulaFields["c10"].Text="'"+c10+"'";
				oRpt.DataDefinition.FormulaFields["giamdoc"].Text="";
				oRpt.DataDefinition.FormulaFields["phutrach"].Text="";
				oRpt.DataDefinition.FormulaFields["thongke"].Text="";
				oRpt.DataDefinition.FormulaFields["ketoan"].Text="";
				oRpt.DataDefinition.FormulaFields["thukho"].Text="";
				if (phieulinh.IndexOf(ReportFile+",")!=-1)
				{
					oRpt.DataDefinition.FormulaFields["l_soluong"].Text=i_soluong_le.ToString();
					oRpt.DataDefinition.FormulaFields["l_dongia"].Text=i_dongia_le.ToString();
					oRpt.DataDefinition.FormulaFields["l_thanhtien"].Text=i_thanhtien_le.ToString();
				}
				Report.ReportSource=oRpt;
			}
			catch (Exception e)
			{
				MessageBox.Show ("The following error was discovered: '"+e.Message+"'. It was occured in '"+e.StackTrace+"'", "Report Viewer",MessageBoxButtons.OK, MessageBoxIcon.Error);				
			}
		}

		private void butKetthuc_Click(object sender, System.EventArgs e)
		{
			bPrinter=false;
            if (Report != null) Report.Dispose();
			this.Close();
		}

		public void butIn_Click(object sender, System.EventArgs e)
		{
			Cursor=Cursors.WaitCursor;
			bPrinter=true;
			try
			{
				oRpt.PrintToPrinter(Convert.ToInt16(banin.Value),false,Convert.ToInt16(tu.Value),Convert.ToInt16(den.Value));
			}
			catch(Exception ex){MessageBox.Show(ex.Message);}
			Cursor=Cursors.Default;
			this.Close();
		}

		private void frmReport_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			switch (e.KeyCode)
			{
				case Keys.F9: butIn_Click(sender,e);break;
				case Keys.F7: butExcel_Click(sender,e);break;
				case Keys.F10: butKetthuc_Click(sender,e);break;
			}
		}

		private void banin_KeyDown(object sender, System.Windows.Forms.KeyEventArgs e)
		{
			if (e.KeyCode==Keys.Enter) SendKeys.Send("{Tab}");
		}

		private void butExcel_Click(object sender, System.EventArgs e)
		{        
			m.check_process_Excel();
			string tenfile=m.Export_Excel(ds.Tables[0],ReportFile.Substring(0,ReportFile.Length-4));
			oxl=new Excel.Application();
            owb = (Excel._Workbook)(oxl.Workbooks.Open(tenfile, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value, Missing.Value));
			osheet=(Excel._Worksheet)owb.ActiveSheet;
			oxl.ActiveWindow.DisplayGridlines=true;
			oxl.ActiveWindow.DisplayZeros=false;
			osheet.PageSetup.Orientation=XlPageOrientation.xlLandscape;
			osheet.PageSetup.PaperSize=XlPaperSize.xlPaperA4;
			osheet.PageSetup.LeftMargin=20;
			osheet.PageSetup.RightMargin=20;
			osheet.PageSetup.TopMargin=30;
			osheet.PageSetup.CenterFooter="Trang : &P/&N";
			oxl.Visible=true;
		}

        public frmReport(AccessData acc, DataSet _ds, string report, string mMsg, string tTitle,string ten_nd)
        {
            InitializeComponent();
            ReportFile = report;
            ds = _ds;
            dt = _ds.Tables[0];
            msg = mMsg;
            msg1 = tTitle;
            m = acc;
            nguoi_lb = acc.get_tennvdangnhap(ten_nd.Trim());
        }

        public frmReport(AccessData acc, DataSet dset, string mMsg, string _ngaythangnam, string _ngayin, string _bophan, string report,string ten_nd)
        {
            InitializeComponent();
            nhanluckhoasan = true;
            m = acc;
            ds = dset;
            msg = mMsg;
            ngaythangnam = _ngaythangnam;
            ngayin = _ngayin;
            bophan = _bophan;
            ReportFile = report;
            this.Text = report;
            nguoi_lb = acc.get_tennvdangnhap(ten_nd.Trim());
        }
	}
}